package com.lesserpand.springcloudalibabasso.feign;

import com.lesserpand.springcloudalibabasso.fallback.SecurityOauthFeignFallbackFactory;
import com.lesserpand.springcloudalibabasso.security.entity.AccessTokenBO;
import com.lesserpand.springcloudalibabasso.security.entity.SecurityOauthThreeDO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author bailuo
 * @version 2.0
 * @Description TODO
 * @Date Created in 2018/12/11 12:04
 */
@FeignClient(value = "spring-cloud-alibaba-provider", fallbackFactory = SecurityOauthFeignFallbackFactory.class)
public interface SecurityOauthFeign {


    /**
     * 授权码模式，获取 code，回调暂时不行
     *
     * @param clientId    clientId
     * @param redirectUri redirectUri
     * @return access token
     */
    @PostMapping(value = "/oauth/authorize?response_type=code", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    String authorize(@RequestParam(value = "client_id") String clientId,
                     @RequestParam(value = "redirect_uri") String redirectUri);

    /**
     * 授权码模式，获取 token，回调暂时不行
     *
     * @param code         code
     * @param clientId     clientId
     * @param clientSecret clientSecret
     * @param redirectUri  redirectUri
     * @return access token
     */
    @PostMapping(value = "/oauth/token?grant_type=authorization_code", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    SecurityOauthThreeDO getAccessToken(@RequestParam(value = "code") String code,
                                        @RequestParam(value = "client_id") String clientId,
                                        @RequestParam(value = "client_secret") String clientSecret,
                                        @RequestParam(value = "redirect_uri") String redirectUri);


    /**
     * 获取 token
     *
     * @param username username
     * @param password passowrd
     * @return access token
     */
    @PostMapping(value = "/oauth/token?grant_type=password", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    AccessTokenBO getAccessTokenByPassword(@RequestParam(value = "username") String username,
                                           @RequestParam(value = "password") String password);

}
